﻿package mortal.game.view.guide
{
    import Message.DB.Tables.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.event.*;
    import frEngine.pools.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.player.*;
    import mortal.game.view.common.*;

    public class GuideWelcomeWin extends GAlertWin implements IWindow3D
    {
        private var _container:Sprite;
        private var _container3D:Sprite;
        private var _bg:GImageBitmap;
        private var _btn:GLoadingButton;
        private var _rect3d:Rect3DObject;
        private var _bodyPlayer:WizardActionPlayer;
        private var timerContorler:TimeControler;
        private var _timer:Timer;

        public function GuideWelcomeWin(param1:Number, param2:Number)
        {
            this._container = new Sprite();
            this._container3D = new Sprite();
            super(param1, param2);
            this.configEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
            this.configEventListener(Event.REMOVED_FROM_STAGE, this.removeFromStageHandler);
            return;
        }// end function

        private function removeFromStageHandler(event:Event) : void
        {
            Game.scene.setMouseEnabled(true);
            return;
        }// end function

        private function addToStageHandler(event:Event) : void
        {
            this.x = Global.stage.stageWidth / 2 - 230;
            this.y = Global.stage.stageHeight / 2 - 244;
            this.dispatchEvent(new WindowEvent(WindowEvent.POSITIONCHANGE));
            Game.scene.setMouseEnabled(false);
            this.createTimer();
            return;
        }// end function

        private function timerEnd(param1 = null) : void
        {
            this.clickMeHandler(null);
            return;
        }// end function

        private function disposeTimner() : void
        {
            if (this._timer != null)
            {
                this._timer.removeEventListener(TimerEvent.TIMER, this.timerEnd);
                this._timer.stop();
                this._timer = null;
            }
            return;
        }// end function

        private function createTimer() : void
        {
            if (this._timer == null)
            {
                this._timer = new Timer(8000);
                this._timer.addEventListener(TimerEvent.TIMER, this.timerEnd);
            }
            this._timer.start();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseEnabled = true;
            this.mouseChildren = true;
            this.timerContorler = TimeControler.createTimerInstance();
            this.timerContorler.unActive();
            this._bg = UIFactory.gImageBitmap(null, 0, 0, this);
            LoaderHelp.setBitmapdata("WelcomeBg.swf", this._bg);
            this._btn = UIFactory.gLoadingButton(ImagesConst.WelComeEnterGameBtn_upSkin, 236, 360, 113, 114, this);
            this.add3d();
            this._btn.configEventListener(MouseEvent.CLICK, this.clickMeHandler);
            this.configEventListener(MouseEvent.CLICK, this.clickMeHandler);
            Global.stage.addEventListener(MouseEvent.CLICK, this.clickMeHandler);
            return;
        }// end function

        private function clickMeHandler(event:MouseEvent) : void
        {
            if (this.parent == null)
            {
                return;
            }
            if (closeHandler != null)
            {
                if (extendObj != null)
                {
                    closeHandler(Alert.OK, extendObj);
                }
                else
                {
                    closeHandler(Alert.OK);
                }
            }
            this.parent.removeChild(this);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            Global.stage.removeEventListener(MouseEvent.CLICK, this.clickMeHandler);
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            if (this._bodyPlayer)
            {
                this.timerContorler.unActive();
                FrEventDispatcher.instance.proxyRemoveEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                this._bodyPlayer.dispose(true);
                this._bodyPlayer = null;
            }
            this.disposeTimner();
            return;
        }// end function

        public function get contentContainer() : Sprite
        {
            return this._container;
        }// end function

        private function add3d() : void
        {
            this._rect3d = Rect3DManager.instance.getRect3dByWindow(this);
            var _loc_1:* = ModelConfig.instance.getInfoByCode(int(WizardConfig.instance.getSpriteByCodeAndLevel(1, 1).modelId));
            var _loc_2:* = _loc_1.mesh1 + ".md5mesh";
            var _loc_3:* = _loc_1.bone1 + ".skeleton";
            if (!this._rect3d)
            {
                this._rect3d = Rect3DManager.instance.registerWindow(false, new Rectangle(-80, 190, 10, 10), this);
            }
            Rect3DManager.instance.windowShowHander(null, this);
            if (this._bodyPlayer == null)
            {
                this._bodyPlayer = FrObjectPool.getObject(WizardActionPlayer, this);
                FrEventDispatcher.instance.proxyAddEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                this._bodyPlayer.timerContorler = this.timerContorler;
            }
            this.timerContorler.active();
            this._bodyPlayer.clearExtend();
            this._bodyPlayer.load(_loc_2, _loc_3, _loc_1.texture1, this._rect3d.renderList);
            if (_loc_1.mesh2)
            {
                _loc_2 = _loc_1.mesh2 + ".md5mesh";
                _loc_3 = _loc_1.bone2 + ".skeleton";
                this._bodyPlayer.loadExtend(_loc_2, _loc_3, _loc_1.texture2, this._rect3d.renderList);
                this._bodyPlayer.extendPlayer.timerContorler = this.timerContorler;
            }
            this._bodyPlayer.play();
            this._bodyPlayer.scaleValue = 4;
            this._bodyPlayer.rotationY = 0;
            this._rect3d.addObject3d(this._bodyPlayer, 0, 370, false);
            this._bodyPlayer.changeAction(ActionName.Standing);
            return;
        }// end function

        override protected function getButtonFlagsList() : Array
        {
            return [];
        }// end function

        protected function onInitBodyPlayer(event:Event) : void
        {
            if (this._bodyPlayer)
            {
                this._bodyPlayer.addFrameComplete(ActionName.Standing, this.onFrameComplete);
            }
            return;
        }// end function

        private function onFrameComplete() : void
        {
            if (!this._bodyPlayer)
            {
                return;
            }
            this._bodyPlayer.changeAction(ActionName.Stand);
            return;
        }// end function

        public function get contentTopOf3DSprite() : Sprite
        {
            return this._container3D;
        }// end function

    }
}
